package com.antt.cn.test2.uis.activities;

import android.os.Environment;
import android.view.View;

import com.antt.cn.test2.R;
import com.antt.cn.test2.uis.base.BaseActivity;
import com.antt.cn.test2.uis.views.ReaderTextView;
import com.antt.cn.test2.utils.AntLog;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by baichuan on 2017/4/24.
 */

public class Test03 extends BaseActivity {
    private String ss = "";

    @BindView(R.id.ac_test03_textView)
    ReaderTextView text;

    @Override
    public int getLayoutId() {
        return R.layout.ac_test03_layout;
    }

    @Override
    public void initView() {
        ButterKnife.bind(this);
        text.setText(ss);
    }

    @Override
    public void initData() {
        getData();
    }


    public void getData() {

        new Observable<String>() {
            @Override
            protected void subscribeActual(Observer<? super String> observer) {
                try {
                    AntLog.i("thread==" + Thread.currentThread().getName() + ",  id=" + Thread.currentThread().getId());
                    File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/bcreader/book.txt");
                    boolean exists = file.exists();
                    String s = FileUtils.readFileToString(file, "UTF-8");
                    observer.onNext(s);
                    observer.onComplete();
                } catch (IOException e) {
                    e.printStackTrace();
                    observer.onError(e);
                }
            }
        }.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<String>() {
                    @Override
                    public void accept(@NonNull String s) throws Exception {
                        ss = s;
                        text.setData(ss);
                    }
                });
    }


    @Override
    public void initListener() {

    }


    @OnClick(R.id.ac_test03_obt01)
    public void test(View v) {
        AntLog.i("button click");
        text.nextPage();
    }

}
